package com.hit.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hit.commonUtils.R;
import com.hit.eduService.entity.EduChapter;
import com.hit.eduService.entity.EduVideo;
import com.hit.eduService.entity.chapter.ChapterVo;
import com.hit.eduService.entity.chapter.VideoVo;
import com.hit.eduService.mapper.EduChapterMapper;
import com.hit.eduService.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.eduService.service.EduVideoService;
import com.hit.serviceBase.exceptionHandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;

    //根据课程id查询课程的章节和对应的小节
    public List<ChapterVo> getChapterVideoById(String courseId){
        List<ChapterVo> ans=new ArrayList<>();
        //1.首先到edu_chapter表中查出对应课程id的所有章节
        QueryWrapper<EduChapter> wrapperChapter=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);
        //2.到edu_video表中查出对应课程id的所有小节
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(wrapperVideo);
        //3.构造出要return的List中的所有章节
        for(int i=0;i<eduChapters.size();i++){
            EduChapter  eduChapter=eduChapters.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            ans.add(chapterVo);
        }
        //4.把课程id对应的所有小节添加到正确的章节中
        for(int i=0;i<eduVideos.size();i++){//枚举所有小节
            EduVideo eduVideo=eduVideos.get(i);
            //查找这个小节是哪个章节的
            for(int j=0;j<ans.size();j++){
                ChapterVo chapterVo=ans.get(j);
                if(chapterVo.getId().equals(eduVideo.getChapterId())){
                    //把这个eduVideo中所需的字段加入到VideoVo对象中
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //把videoVo加入到对应的ChapterVo对象中
                    chapterVo.getVideos().add(videoVo);
                }
            }
        }
        return ans;
    }

    //删除章节,删除时只删章节则这一章的小节还在数据库中，解决：删除章节时判断这一章有没有小节，只要还有没删除的小节这一章就不能删除
    @Override
    public boolean deleteChapter(String chapterId) {//1589515306840121346
        //首先根据chapterId在edu_video表中查这一章是否存在小节
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int cnt=eduVideoService.count(wrapper);
        if(cnt==0){
            int num=baseMapper.deleteById(chapterId);
            return num>0;
        }else throw new MyException(20001,"本章节还有小节没有被删除，所以不能删除本章节");
    }

    //根据课程id删除对应的全部章节
    @Override
    public void deleteChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }
}
